﻿/*
 * Cratos Server - Windows Service
 * Cratos Start class
 * 
 * Copyright (c) 2011 Draco
 * 
 * This file is part of Cratos.
 * 
 * Cratos is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * Cratos is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Cratos.  If not, see <http://www.gnu.org/licenses/>.
 * 
 *       .==.        .==.
 *      //'^\\      //^'\\
 *     // ^ ^\(\__/)/^ ^^\\
 *    //^ ^^ ^/6  6\ ^^ ^ \\
 *   //^ ^^ ^/( .. )\^ ^ ^ \\
 *  // ^^ ^/\| V""V|/\^ ^ ^ \\
 * //^^/\/ /  '~~'  \ \/\^ ^ \\
 *  ------------------------------------
 *  HERE BE DRAGONS
 *  
 *  Johan Dercksen - johan@draco.co.za
 */
using System;
using System.Linq;
using System.Text;
using System.Configuration;
using System.ServiceProcess;
using System.Collections.Generic;

using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;

using Cratos.Desktop;
using Cratos.Tools.DataObject;

namespace Cratos.Server.WinService
{
	/// <summary>
	/// Cratos Start class
	/// </summary>
	static class CratosStart
	{
		#region Class Constant declarations
		#endregion

		#region Class Variable declarations
		private static CratosWinService m_CratosWinService;
		#endregion

		#region Class Constructors & Destructors
		#endregion

		#region Class Properties
		/// <summary>
		/// Service indicator
		/// </summary>
		public static bool IsService { get; set; }

		/// <summary>
		/// Application Unity Container
		/// </summary>
		public static IUnityContainer UnityContainer { get; set; }
		#endregion

		#region Class Methods
		#endregion

		#region Application Static Thread Methods
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		static void Main(string[] args)
		{
			// Declare the required variables
			ServiceBase[] servicesToRun;

			// Setup the Framework
			CratosHelper.LogFilename = ConfigurationManager.AppSettings["LogFilename"];

			//try
			//{
				// Create and Setup the Unity Container
				CratosStart.UnityContainer = new UnityContainer().LoadConfiguration();

				// Handle the relevant command line arguments
				if ((args.Length != 0) && (args[0] == "/console"))
				{
					// Set the service indicator
					CratosStart.IsService = false;

					// Raptor Service
					Console.WriteLine(string.Format("Cratos Windows Service (Version {0})", CratosHelper.AssemblyVersion));
					Console.WriteLine("       .==.        .==.");
					Console.WriteLine(@"      //'^\\      //^'\\");
					Console.WriteLine(@"     // ^ ^\(\__/)/^ ^^\\");
					Console.WriteLine(@"    //^ ^^ ^/6  6\ ^^ ^ \\");
					Console.WriteLine(@"   //^ ^^ ^/( .. )\^ ^ ^ \\");
					Console.WriteLine(@"  // ^^ ^/\| V""V|/\^ ^ ^ \\");
					Console.WriteLine(@" //^^/\/ /  '~~'  \ \/\^ ^ \\");
					Console.WriteLine(@"  ------------------------------------");
					Console.WriteLine(@"  HERE BE DRAGONS");
					Console.WriteLine();
					Console.WriteLine("Starting the Cratos Service(s) ...");
					Console.WriteLine("Press ENTER to terminate the service");
					Console.WriteLine();

					// Create and setup the service to start
					m_CratosWinService = new CratosWinService();

					// Start the service
					m_CratosWinService.StartService();

					// Wait for the user to close the session
					Console.ReadLine();

					// Stop the service
					m_CratosWinService.StopServices();
				}
				else
				{
					// Set the service indicator
					CratosStart.IsService = true;

					// Create and setup the relevant services to be run
					servicesToRun = new ServiceBase[] { new CratosWinService() };

					// Start the execution of the services
					ServiceBase.Run(servicesToRun);
				}
			//}
			//catch (Exception runtimeException)
			//{

			//}

			// Return to OS
			return;
		}
		#endregion
	}
}
